import mainRoot from "../../../Game/MainRoot";
import O1HD from "../../../engines/gameEngine";
import data_control from "../../../engines/libs/data_control";
import level_goldMiner from "../level_goldMiner";

const {ccclass, property} = cc._decorator;

@ccclass
export default class level_goldMiner_shopItem extends cc.Component {

    @property({type:cc.Sprite,tooltip:"icon节点"})
    icon : cc.Sprite = null;

    @property({type:cc.Label,tooltip:"名字文本"})
    lbName : cc.Label = null;

    @property({type:cc.Node,tooltip:"广告节点"})
    adNode:cc.Node = null;

    @property({type:cc.Node,tooltip:"钱节点"})
    moneyNode:cc.Node = null;

    @property({type:cc.Node,tooltip:"售罄节点"})
    soldOutNode:cc.Node = null;

    @property({type:cc.Label,tooltip:"描述文本"})
    lbDesc:cc.Label = null;

    @property({type:cc.Node,tooltip:"光圈节点"})
    lightNode:cc.Node = null;

    data : any = null;

    buyFlag:boolean = false;

    moneyNum:number = 0;

    level:level_goldMiner = null;

    bindData(data:any,level:level_goldMiner){
        this.data = data;
        this.level = level;
        this.adNode.active = false;
        this.moneyNode.active = false;
        let ad_flag = this.data["ad_flag"];
        this.lightNode.active = false;
        if(ad_flag == "1"){
            this.adNode.active = true;
            this.lightNode.active = true;
            this.lightAni();
        }else{
            this.moneyNode.active = true;
            let minMoney:number = Number(this.data["prop_min_price"]);
            let maxMoney:number = Number(this.data["prop_max_price"]);
            this.moneyNum = Math.floor(data_control.getRandNumberOfArrayTwo([minMoney,maxMoney]));
        }
        this.loadIcon();
    }

    lightAni(){
        let tween = cc.tween(this.lightNode).to(4,{angle:360}).call(()=>{
            this.lightNode.angle = 0;
        });
        cc.tween(this.lightNode).repeatForever(tween).start();
    }

    loadIcon(){
        this.lbName.string = this.data["prop_name"];
        this.lbDesc.string = this.data["prop_desc"];
        let lbMoney:cc.Label = this.moneyNode.getChildByName("lbMoney").getComponent(cc.Label);
        lbMoney.string = this.moneyNum.toString();
        mainRoot.LevelSystem.getOneTypeBundle(this.level.getLevelInfo().levelType).load(`itemIcon/${this.data["sp_path"]}`,cc.SpriteFrame,(err,spr:cc.SpriteFrame)=>{
            if(err){
                console.log(err,"加载icon失败");
                return;
            }
            this.icon.spriteFrame = spr;
        });
    }

    onBuy(){
        this.buyFlag = true;
        this.adNode.active = false;
        this.moneyNode.active = false;
        this.soldOutNode.active = true;
    }
}
